package com.example.lotterysystem.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Configuration
public class RedisUtil {
    private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    /**
     * RedisTemplate :  先将被存储的数据转换成 字节数组（不可读），再存储到redis中，读取的时候按照字节数组读取
     * StringRedisTemplate ： 直接存放的就是 string (可读)
     * 项目背景：String,String
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 设置值
     * @param key
     * @param value
     * @return boolean
     */
    public boolean set(String key,String value){
        try {
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            logger.error("RedisUtil set: ",e);
            logger.error("RedisUtil set:{},{} ",key,value);
            return false;
        }
    }
    /**
     * 设置值(含有过期时间)
     * @param key
     * @param value
     * @param time
     * @return boolean
     */
    public boolean set(String key,String value,Long time){
        try {
            redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            logger.error("RedisUtil set: ",e);
            logger.error("RedisUtil set:{},{} ",key,value);
            return false;
        }
    }
    /**
     * 获取值
     * @param key
     * @return String
     */
    public String get(String key){
        //先判断key是否为空
        try {
            return (StringUtils.hasText(key)?redisTemplate.opsForValue().get(key):null);
        }catch (Exception e){
            logger.error("RedisUtil get: ",e);
            logger.error("RedisUtil get: {} ",key);
            return null;
        }
    }
    /**
     * 删除值,同时可以删除多个
     * @param key
     * @return boolean
     */
    public boolean del(String... key){
        //要先将传入的多个key转换为链表,才能进行操作
        //String... 表示传入多个key,同时key的类型都是String
        try {
            if(key!=null&&key.length>0){
                //有内容
                if (key.length==1){
                    redisTemplate.delete(key[0]);
                }else {
                    redisTemplate.delete((Collection<String>)CollectionUtils.arrayToList(key));
                }
            }
            return true;
        }catch (Exception e){
            logger.error("RedisUtil del: ",e);
            logger.error("RedisUtil del: {}",key);
            return false;
        }
    }
    /**
     * 判断key是否存在
     * @param key
     * @return boolean
     */
    public boolean hasKey(String key){
        try {
            return StringUtils.hasText(key)?
                    redisTemplate.hasKey(key):false;
        }catch (Exception e){
            logger.error("RedisUtil hasKey: ",e);
            logger.error("RedisUtil hasKey: {}",key);
            return false;
        }
    }

}
